package com.lupan.mutithred.cp2_CountDownLatch;

import java.util.concurrent.CountDownLatch;

/**
 * 说明：
 *
 * @author lupan
 * @version 2017/4/11 15:43
 */
public class MyThread extends Thread {

    private CountDownLatch comingTag;
    private CountDownLatch waitTag;
    private CountDownLatch waitRunTag;
    private CountDownLatch beginTag;
    private CountDownLatch endTag;

    public MyThread(CountDownLatch comingTag, CountDownLatch waitTag, CountDownLatch waitRunTag, CountDownLatch beginTag, CountDownLatch endTag) {
        this.comingTag = comingTag;
        this.waitTag = waitTag;
        this.waitRunTag = waitRunTag;
        this.beginTag = beginTag;
        this.endTag = endTag;
    }


    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName() + " 运动员正在到达起跑点。。。");
            Thread.sleep((int) (Math.random() * 10000));
            System.out.println(Thread.currentThread().getName() + " 到达起跑点！等待裁判说起跑。。。");
            comingTag.countDown();
            waitTag.await();
            System.out.println(Thread.currentThread().getName() + " 准备起跑姿势。。。");
            Thread.sleep((int) (Math.random() * 10000));
            waitRunTag.countDown();
            beginTag.await();
            System.out.println(Thread.currentThread().getName() + " 运动员起跑，正在向终点。。。");
            Thread.sleep((int) (Math.random() * 10000));
            System.out.println(Thread.currentThread().getName()+" 到达终点！");
            endTag.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
